package com.oyjd.ybx.fw.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 获取图形验证码
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * 
 */
public class CheckPic {
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		for (int i = 1; i <= 100; i++) {
			ImgVO vo = getImage();
			System.out.println(vo.getCode());
			java.io.FileOutputStream fos = new FileOutputStream(new File("D:/tmp/test" + i + ".jpg"));
			ImageIO.write(vo.getImg(), "JPEG", fos);
		}
	}

	private static List<ImgVO> cacheImgs = new ArrayList<ImgVO>();

	/**
	 * 获取随机图片
	 * 
	 * @param width
	 * @param height
	 * @return
	 */
	public synchronized static ImgVO getImage() {
		if (cacheImgs.size() <= 0) {
			for (int i = 0; i < 50; i++) {
				cacheImgs.add(createImg());
			}
		}
		ImgVO vo = cacheImgs.get(0);
		cacheImgs.remove(vo);
		return vo;
	}

	/**
	 * 获取随机颜色
	 * 
	 * @param fc
	 * @param bc
	 * @return
	 */
	private static Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	/**
	 * 图片创建
	 * 
	 * @return
	 */
	private static ImgVO createImg() {
		int width = 93;
		int height = 37;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		Random random = new Random();
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman", Font.PLAIN + Font.ITALIC , 22));
		g.setColor(getRandColor(160, 200));

		for (int i = 0; i < 100; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}

		String code = RandUtil.get(4);
		for (int i = 0; i < 4; i++) {
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
			g.drawString(Character.toString(code.charAt(i)), 20 * i + 10, 25);
		}
		g.dispose();

		return new ImgVO(image, code);
	}

	/**
	 * 内部临时对象
	 * 
	 * @author Administrator
	 * 
	 */
	public static class ImgVO {
		private BufferedImage img;
		private String code;

		public ImgVO(BufferedImage img, String code) {
			super();
			this.img = img;
			this.code = code;
		}

		public BufferedImage getImg() {
			return img;
		}

		public void setImg(BufferedImage img) {
			this.img = img;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

	}
}
